using JetBrains.Annotations;
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using Mod.Training.Models;
using Mod.Training.Service;
using System;

namespace Mod.Training.Handlers
{
    [UsedImplicitly]
	
    public class ItemListPartHandler : ContentHandler
    {
        public ItemListPartHandler(IRepository<ItemListPartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
			
            //OnPublished<ItemListPart>
        }
    }

    public class ItemCreationHandler : ContentHandler
    {
        private readonly Lazy<ITrainingService> tService;

        public ItemCreationHandler(Lazy<ITrainingService> tService)
        {
            this.tService = tService;

            OnUpdated<ItemListPart>((ctx, part) => CreateItems(part));
        }

        private void CreateItems(ItemListPart part)
        {
            foreach (var item in part.Items)
            {
                item.ItemListPartRecord = part.Record;
                tService.Value.CreateItem(item);
            }
        }
    }
}
